package com.example.char04;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.text.format.Formatter;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.example.administrator.mysaft.R;

import java.util.ArrayList;

import java.util.List;


public class AppManagerActivity extends AppCompatActivity implements View.OnClickListener {
    /**
     * 手机剩余内存TEXTEview
     */
    private TextView mPhoneMemoryTV;
    /**
     * 展示剩余内存
     */
    private TextView mSDMemoryTV;
    private ListView mListView;
    private List<AppInfo> appInfos;
    private List<AppInfo> userAppInfos = new ArrayList<AppInfo>();
    private List<AppInfo> systemAppInfos = new ArrayList<AppInfo>();
    private APPMangerAdapter adapter;
    /**
     * 接收应用程序卸载成功广播
     */
    private UninstallReciver receciver;
    private TextView mAppNumtv;
    private Handler mHandler=new Handler() {

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */


    public void handleMessage( Message msg) {
        switch (msg.what) {
            case 10:
                if (adapter == null) {
                    adapter = new APPMangerAdapter(userAppInfos,
                            systemAppInfos, AppManagerActivity.this);
                }
                        mListView.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        break;
                        case 15:
                            adapter.notifyDataSetChanged();
                            break;

                    }
                };
        };

//           @Override
//           protected  void onCreate(Bundle savedInstanceState){
//            super.onCreate(SaveInstanceState);
////            requestWindowFeature(Window().FEATURE_NO_TITLE);
//            setContentView(R.layout.activity_app_manager);
//            //注册广播
//            receciver=new UninstallReciver();
//            IntentFilter intentFilter=new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
//            IntentFilter.addDataScheme("package");
//            registerReceiver(receciver,intentFilter);
//            initView();
//        }


    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //注册广播
        receciver=new UninstallReciver();
        IntentFilter intentFilter=new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
//        IntentFilter.addDataScheme("package");
               intentFilter.addDataScheme("package");
        registerReceiver(receciver,intentFilter);
        initView();
    }

    /**初始化控件*/
private void initView(){
        findViewById(R.id.rl_titlebar).setBackgroundColor(getResources().getColor(R.color.bright_yellow));
    ImageView mLeftImgv=(ImageView)findViewById(R.id.imgv_leftbtn);
        ((TextView)findViewById(R.id.tv_title)).setText("软件管家");
        mLeftImgv.setOnClickListener(this);
//        mLeftImgv.setImageResource(R.drawable.black);
        mPhoneMemoryTV=(TextView)findViewById(R.id.tv_phonememory_appmanager);
        mSDMemoryTV=(TextView)findViewById(R.id.tv_sdmemory_appmanager);
       mListView=(ListView)findViewById(R.id.lv_appnumber);
    mAppNumtv=(TextView)findViewById(R.id.tv_appnumber);
        //获取手机剩余的内存SD卡内存
        getMemoryFromPhone();
        initData();
        initListener();
    }
    private void initListener(){
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                if (adapter != null) {
                    new Thread() {
                        public void run() {
                            AppInfo mappInfo = (AppInfo) adapter.getItem(position);
                            //记住当前条目的AppInfo
                            boolean flag = mappInfo.isSelected;
                            //现将集合中所有条目的Appinfo变为未选中的状态
                            for (AppInfo appInfo : userAppInfos) {
                            mappInfo.isSelected = false;
                        }

                       for(AppInfo appInfo:systemAppInfos )

                        {
                            appInfo.isSelected = false;
                        }

                        if(mappInfo!=null)

                        {
                            //如果已经选中，则变为未选中

                        if(flag){
                            mappInfo.isSelected = false;
                        }else{
                            mappInfo.isSelected = true;

                        }


                            mHandler.sendEmptyMessage(15);
                    }
                } ;
            }.start();
        }
    }
        });
       mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
           @Override
           public void onScrollStateChanged(AbsListView view, int scrollState) {

           }

           @Override
           public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
               if(firstVisibleItem>=userAppInfos.size()+1){
                   mAppNumtv.setText("系统程序:"+systemAppInfos.size()+"个");
               }else{
                   mAppNumtv.setText("用户程序:"+userAppInfos.size()+"个");

               }
           }
       });
        }
  private  void  initData(){
      appInfos=new ArrayList<AppInfo>();
      new Thread(){
          public  void  run(){
              appInfos.clear();
              systemAppInfos.clear();
              appInfos.addAll(AppInfoParser.getAppInfos(AppManagerActivity.this));
              for (AppInfo appInfo:appInfos){
                  //如果是用户APP
                  if (appInfo.isUserApp){
                      userAppInfos.add(appInfo);
                  }else{
                      systemAppInfos.add(appInfo);
                  }
              }
              mHandler.sendEmptyMessage(10);
          };
      }.start();
  }
    /**拿到手机SD卡剩余内存*/
    private void getMemoryFromPhone(){
        long avail_sd= Environment.getExternalStorageDirectory().getFreeSpace();
        long avail_rom= Environment.getDataDirectory().getFreeSpace();
        //格式化内存
        String str_avail_sd= Formatter.formatFileSize(this,avail_sd);
        String str_avail_rom=Formatter.formatFileSize(this,avail_rom);
        mSDMemoryTV.setText("剩余SD卡内存:"+str_avail_sd);
    }

    public void onClick(View v){
    switch (v.getId()){
        case  R.id.imgv_leftbtn:
            finish();
            break;
    }
    }



    /**接收应用程序卸载的*/
    class  UninstallReciver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context,Intent intent){
            //接到广播
            initData();
        }
    }
    @Override
    protected  void onDestroy(){
        unregisterReceiver(receciver);
        receciver=null;
        super.onDestroy();
}

    }






